package edu.ccut.saturn.studio.wfg.xml.loader;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import edu.ccut.saturn.studio.wfg.model.Shape;
import edu.ccut.saturn.studio.wfg.wfgmodel.*;

public class SaturnStartNodeLoader extends NodeLoader {

	@Override
	public List<Shape> loadNodes(Element root) {
		NodeList startNodes = root.getElementsByTagName(SaturnName.XML_NAME);
		List<Shape> startList = new ArrayList<Shape>();
		for(int i = 0 ; i<startNodes.getLength();i++){
			Node start = startNodes.item(i);
			if(SaturnName.KEY_NODE_ATTRIBUTE_TYPENAMESTART.equals(super.getNodeAttributeValue(start, SaturnName.XML_TYPE))){
				SaturnStartNode saturnStart = new SaturnStartNode();
				// 基本信息
				saturnStart.setName(super.getNodeElementValue(start, Shape.KEY_NODE_NAME));
				saturnStart.setId(super.getNodeAttributeValue(start, Shape.KEY_ATTRIBUTE_ID));
				saturnStart.setNextId(super.getNodeAttributeValue(start, Shape.KEY_ATTRIBUTE_SHAPE_NEXT_ID));
				saturnStart.setLocation(super.getLocation(start));
				saturnStart.setSize(super.getSize());
				saturnStart.setStartPolicy(super.getNodeElementValue(start, SaturnName.KEY_NODE_ELEMENT_STARTPOLICY));
				saturnStart.setTimeLimit(super.getNodeElementValue(start, SaturnName.KEY_NODE_ELEMENT_TIMELIMIT));
				saturnStart.setOffsetMode(super.getNodeElementValue(start, SaturnName.KEY_NODE_ELEMENT_OFFSETMODE));
				saturnStart.setDescription(super.getNodeElementValue(start, SaturnStartNode.KEY_NODE_DESCRIPTION));
				// logicKey扩展逻辑
				saturnStart.setLogicKeyList(super.getSaturnAttributeList(start, SaturnName.KEY_NODE_ELEMENT_LOGICKEY, SaturnName.KEY_NODE_NAME_KEY, SaturnName.METHOD_GETKEY));
				// formData启动参数
				saturnStart.setFormDataAttributeList(super.getSaturnAttributeList(start, SaturnName.KEY_NODE_ELEMENT_FORMDATA, SaturnName.KEY_NODE_NAME_DATA,SaturnName.METHOD_GETSATURNATTRIB));
				// 多工作项 暂无
				// 启动限制
				saturnStart.setSaturnStartLimit(getStartLimit(start));
				
				startList.add(saturnStart);
			}
		}
		
		return startList;
	}
	
	private SaturnStartLimit getStartLimit(Node node){
		SaturnStartLimit startLimit = new SaturnStartLimit();
		NodeList nodelist = node.getChildNodes();
		for(int i = 0;i<nodelist.getLength();i++){
			if(nodelist.item(i).getNodeName().equals(SaturnName.KEY_NODE_ELEMENT_STARTLIMIT)){
				NodeList startLimitList = nodelist.item(i).getChildNodes();
				for(int j = 0 ;j<startLimitList.getLength();j++){
					if(startLimitList.item(j).getNodeName().equals(SaturnName.KEY_NODE_NAME_ID)){
///**有问题*/					String id = super.getNodeElementValue(startLimitList.item(j),StartLimit.ID );
							String id = startLimitList.item(j).getFirstChild().getNodeValue();
						startLimit.setStartLimitID(id);
					}
					if(startLimitList.item(j).getNodeName().equals(SaturnName.KEY_STARTLIMIT_LOGICKEY)){
						NodeList logicKeyList = startLimitList.item(j).getChildNodes();
						for(int k = 0 ;k<logicKeyList.getLength();k++){
							if(logicKeyList.item(k).getNodeName().equals(SaturnName.KEY_NODE_NAME_FULLKEY)){
//								String fullKey = super.getNodeElementValue(logicKeyList.item(k), StartLimit.FULLKEY);
								if(logicKeyList.item(k).getFirstChild()!=null){
									String fullKey = logicKeyList.item(k).getFirstChild().getNodeValue();
									startLimit.setLogicKey(fullKey);
								}
							}
							if(logicKeyList.item(k).getNodeName().equals(SaturnName.KEY_NODE_NAME_PARAMETER)){
								SaturnAttribute saturn = new ExtendAttributeForLogicKey();
								String parameterId = super.getNodeAttributeValue(logicKeyList.item(k), SaturnName.KEY_NODE_NAME_ID);
								((ExtendAttributeForLogicKey)saturn).setId(parameterId);
								String parameterElement = logicKeyList.item(k).getFirstChild().getNodeValue();
								((ExtendAttributeForLogicKey)saturn).setParameter(parameterElement);
								startLimit.getExtendAttributeForLogicKey().add(saturn);
							}
						}
					}
				}
			}
		}
		
		return startLimit;
	}
}